#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>

int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
    struct termios newtio,oldtio;
    if  ( tcgetattr( fd,&oldtio)  !=  0) 
    { 
        perror("SetupSerial 1");
        return -1;
    }
    bzero( &newtio, sizeof( newtio ) );
    newtio.c_cflag  |=  CLOCAL | CREAD; 
    newtio.c_cflag &= ~CSIZE; 

    switch( nBits )
    {
    case 7:
        newtio.c_cflag |= CS7;
        break;
    case 8:
        newtio.c_cflag |= CS8;
        break;
    }

    switch( nEvent )
    {
    case 'O':                     //奇校验
        newtio.c_cflag |= PARENB;
        newtio.c_cflag |= PARODD;
        newtio.c_iflag |= (INPCK | ISTRIP);
        break;
    case 'E':                     //偶校验
        newtio.c_iflag |= (INPCK | ISTRIP);
        newtio.c_cflag |= PARENB;
        newtio.c_cflag &= ~PARODD;
        break;
    case 'N':                    //无校验
        newtio.c_cflag &= ~PARENB;
        break;
    }

switch( nSpeed )
    {
    case 300:
        cfsetispeed(&newtio, B300);
        cfsetospeed(&newtio, B300);
        break;	
    case 600:
        cfsetispeed(&newtio, B600);
        cfsetospeed(&newtio, B600);
        break;		
    case 1200:
        cfsetispeed(&newtio, B1200);
        cfsetospeed(&newtio, B1200);
        break;			
    case 2400:
        cfsetispeed(&newtio, B2400);
        cfsetospeed(&newtio, B2400);
        break;
    case 4800:
        cfsetispeed(&newtio, B4800);
        cfsetospeed(&newtio, B4800);
        break;
    case 9600:
        cfsetispeed(&newtio, B9600);
        cfsetospeed(&newtio, B9600);
        break;
     case 19200:
        cfsetispeed(&newtio, B19200);
        cfsetospeed(&newtio, B19200);
        break;       
     case 38400:
        cfsetispeed(&newtio, B38400);
        cfsetospeed(&newtio, B38400);
        break;  
     case 57600:
        cfsetispeed(&newtio, B57600);
        cfsetospeed(&newtio, B57600);
        break;      
    case 115200:
        cfsetispeed(&newtio, B115200);
        cfsetospeed(&newtio, B115200);
        break;
    default:
        printf("sorry! [%d bps] is not support ,Set default 9600bps now!\n",nSpeed);
        cfsetispeed(&newtio, B9600);
        cfsetospeed(&newtio, B9600);
        break;
    }
    if( nStop == 1 )
    {
        newtio.c_cflag &=  ~CSTOPB;
    }
    else if ( nStop == 2 )
    {
        newtio.c_cflag |=  CSTOPB;
    }
    newtio.c_cc[VTIME]  = 0;
    newtio.c_cc[VMIN] = 0;
    tcflush(fd,TCIFLUSH);
    if((tcsetattr(fd,TCSANOW,&newtio))!=0)
    {
        perror("com set error");
        return -1;
    }
    printf("set done!\n");
    return 0;
}

int open_port(int comport)//打开串口函数
{
 int fd;
 char *dev[]={"/dev/ttySAC0","/dev/ttySAC1","/dev/ttySAC2","/dev/ttySAC3"};
 long vdisable;
 printf("com:%s open\n",dev[comport]);
 fd=open(dev[comport],O_RDWR|O_NOCTTY);
 if(fd==-1){
  perror("can't open serial port");
  return -1;
 }
 //恢复串口为阻塞状态
 if(fcntl(fd,F_SETFL,0)<0){
	  printf("fcntl failed\n");
 }
 else{
  printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));
 }
 //测试是否为终端设备
 if(isatty(STDIN_FILENO)==0){
  printf("standard input is not a terminal device\n");
 }
 else{
  printf("isatty success\n");
 }
 printf("fd-open=%d\n",fd);
 return fd;
}
int main(void)
{
 int fd;
 int nread,nwrite1,nwrite2,i,n;
 char buff1[20]={0};
 char buff2[20]={0};


 if((fd=open_port(1))<0){//打开串口
  perror("open port error");
  return -1;
 }
 if((i=set_opt(fd,115200,8,'N',1))<0){//设置串口
  perror("set_opt error");
  return -1;
 }
 
 //write some thing
nwrite1 = sprintf(buff1,"%s","#8 p 600");
buff1[nwrite1++] = 0x0d;
buff1[nwrite1++] = 0x0a;

nwrite2 = sprintf(buff2,"%s","#8 p 2400");
buff2[nwrite2++] = 0x0d;
buff2[nwrite2++] = 0x0a;
				
 i=0;
while(i<100){
	n = 0; 
	if(i%2==0){
		n = write(fd, buff1, nwrite1);
		printf("buff1:%s,len:%d",buff1,nwrite1);
	}
	else{
		n = write(fd, buff2, nwrite2);
		printf("buff2:%s,len:%d",buff2,nwrite2);
	}
	if(n == -1)
	{
	fprintf(stderr, "Wirte ATZ command error.\n");
	}else{
	printf("wirte success count:%d\r\n",n);
	}
	i++;
	sleep(4);
	printf("-------------------\r\n");
}
/*
 
   printf("begin read\r\n");
   while(i<100)
   {
	nread=read(fd,buff,20);
    write(fd, "#0 p 1500\r\n", 11);
	printf("read data size:%d\r\n",nread);
	n=0;
	while(n<20){
		printf("[%x]",buff[n]);
		n++;
	}
	i++;
	printf("\r\n");
    printf("nread=%d,%s\r\n",nread,buff);
    memset(buff,0,sizeof(buff));
	sleep(4);
   }
*/

 printf("fd-%d end!!!!\n",fd);
 close(fd);
 return 0;
}
